<template>
  <a-layout id="components-layout-custom-trigger">
    <!-- 你可以把侧边栏菜单放到另外一个components里面这里就不会占太多的位置 -->
    <a-layout-sider v-model="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu
        theme="dark"
        mode="inline"
        :default-selected-keys="defaultSelectKeys"
        :defaultOpenKeys="defaultOpenKeys"
        :selectedKeys="defaultSelectKeys"
        :open-keys="openKeys"
        @openChange="onOpenChange"
      >
        <a-sub-menu
          v-for="item of childrenRoutes"
          :key="item.group"
          @titleClick="titleClick"
        >
          <span slot="title">
            <a-icon :type="item.groupIcon" />
            <span>{{ item.title }}</span>
          </span>
          <a-menu-item
            @click="clickAMenuItemEvent(sitem)"
            v-for="sitem of item.children"
            :key="sitem.path"
          >
            <a-icon :type="sitem.meta.icon" />
            <span>{{ sitem.meta.title }}</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <Header @collapsedEvent="collapsedEvent" :collapsed="collapsed"></Header>
      <a-layout-content
        :style="{ margin: '2px', background: '#fff', minHeight: '280px' }"
        class="silderbar"
      >
        <div class="tabs">
          <div
            :class="
              currentMenuKeys == item.path ? 'active tabs-item' : 'tabs-item'
            "
            v-for="(item, index) of tabs"
            :key="index"
          >
            <span @click="handleRouterChange(item)">{{ item.meta.title }}</span>
            <span style="margin-left: 10px">
              <a-icon @click="closeTabs(item, index)" type="close-circle" />
            </span>
          </div>
        </div>
        <div class="bar">
          <router-view></router-view>
          <a-divider />
          <a-layout-footer>Mr.Y@2020</a-layout-footer>
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import Header from "@/components/Header.vue";
import childrenRoutes from "@/router/children";
// 使用组件
@Component({
  components: {
    Header,
  },
})
export default class Layout extends Vue {
  // debug
  private debugMode: any;
  // 控制擦边栏开关
  private collapsed = false;
  // 子路由
  private childrenRoutes = childrenRoutes.routes;
  // 所有group key
  private rootSubmenuKeys: string[] = [];

  // 输入栏url路由
  private defaultUrlRouter = location.href.split("#")[1];
  // tabs页面
  private tabs: any[] = [this.childrenRoutes[0].children[0]];
  // 当前选中的菜单
  private currentMenuKeys: string = this.childrenRoutes[0].children[0].path;

  // 当前打开的菜单组
  private openKeys = [this.childrenRoutes[0].group];
  // 默认打开的菜单
  private defaultSelectKeys = [this.childrenRoutes[0].children[0].path];
  // 默认打开的菜单组
  private defaultOpenKeys: any = [this.childrenRoutes[0].group];
  // 初始化
  constructor() {
    super();
    let allRoutes: any = [];
    this.childrenRoutes.forEach((ele: any) => {
      ele.children.forEach((item: any) => {
        allRoutes.push(item);
      });
    });
    this.tabs = allRoutes.filter((ele: any) => {
      return ele.path == this.defaultUrlRouter;
    });
    this.currentMenuKeys = this.defaultUrlRouter;

    let openkey: any = this.childrenRoutes.find((ele: any) => {
      return ele.children.find((item: any) => {
        return item.path == this.tabs[0].path;
      });
    });
    this.defaultOpenKeys = [openkey.group];

    this.defaultSelectKeys = [this.tabs[0].path];
    this.openKeys = [openkey.group];

    this.childrenRoutes.forEach((ele: any) => {
      this.rootSubmenuKeys.push(ele.group);
    });
  }
  // 侧边栏开关事件
  public collapsedEvent() {
    this.collapsed = !this.collapsed;
  }
  // mounted生命周期
  public mounted() {
    if (this.debugMode.debug) {
    }
  }

  // 标题点击事件
  public titleClick(e: any) {
    if (this.debugMode.debug) {
      console.log(e);
    }
    this.defaultOpenKeys = [e.key];
    sessionStorage.setItem("defaultOpenKeys", e.key);
  }
  // 菜单点击事件
  public clickAMenuItemEvent(e: any) {
    sessionStorage.setItem("defaultSelectKey", e.path);
    this.addTabs(e);
    this.$router.push(e.path).catch((err: any) => err);
  }
  // 把点击到的菜单添加到tabs
  public addTabs(e: any) {
    sessionStorage.setItem("tabs", JSON.stringify(e));
    this.currentMenuKeys = e.path;
    this.defaultSelectKeys = [e.path];
    if (this.tabs.length == 0) {
      this.tabs.push({
        ...e,
      });
    } else {
      const hasRouter = this.tabs.find((ele: any) => {
        if (ele.path == e.path) {
          return true;
        }
        return false;
      });

      if (hasRouter) {
        return;
      } else {
        this.tabs.push({
          ...e,
        });
      }
    }
    if (this.debugMode.debug) {
      console.log(this.tabs);
    }
  }
  // 打开菜单并关闭上一个打开的菜单
  public onOpenChange(openKeys: any) {
    const latestOpenKey = openKeys.find(
      (key: any) => this.openKeys.indexOf(key) === -1
    );
    if (this.rootSubmenuKeys.indexOf(latestOpenKey) === -1) {
      this.openKeys = openKeys;
    } else {
      this.openKeys = latestOpenKey ? [latestOpenKey] : [];
    }
  }
  // 点击tab切换页面
  public handleRouterChange(e: any) {
    if (this.debugMode.debug) {
      console.log(e);
    }

    sessionStorage.setItem("tabs", JSON.stringify(e));
    this.currentMenuKeys = e.path;
    this.defaultSelectKeys = [e.path];
    // this.childrenRoutes[0].group
    const temp = this.childrenRoutes.filter((ele: any) => {
      const has = ele.children.find((item: any) => item.path === e.path);
      return has;
    });
    // group: "console"
    this.onOpenChange([temp[0].group]);
    if (this.debugMode.debug) {
      console.log(temp);
    }
    this.$router.push(e.path).catch((err: any) => err);
  }
  //关闭tabs
  public closeTabs(item: any, index: number) {
    if (this.debugMode.debug) {
      console.log("鼠标右键点击事件");
    }
    const temp = this.tabs;
    if (temp.length == 1) {
      return;
    }
    const temparr = temp.filter((ele) => {
      return ele.path != item.path;
    });
    this.tabs = temparr;
    if (this.currentMenuKeys == item.path) {
      if (this.tabs[index - 1] !== undefined) {
        this.$router.push(this.tabs[index - 1].path).catch((err: any) => err);
        this.handleRouterChange(this.tabs[index - 1]);
      } else {
        this.$router.push(this.tabs[index].path).catch((err: any) => err);
        this.handleRouterChange(this.tabs[index]);
      }
    }
  }

  /**
   * beforeDestroyed
   */
  public beforeDestroy() {
    sessionStorage.clear();
  }
}
</script>



<style lang='less' scoped>
#components-layout-custom-trigger {
  width: 100vw;
  height: 100vh;
}

#components-layout-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
.tabs {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 8px;
  position: fixed;
  width: 100%;
  max-width: calc(100vw - 205px);
  background: #fff;
  z-index: 999;
  overflow-x: auto;
  .tabs-item {
    padding: 8px 16px;
    color: #999;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;

    span {
      display: flex;
      justify-content: center;
      align-items: center;
      &:first-child {
        min-width: 40px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
      }
    }
  }
  .active {
    color: #1890ff;
  }
}
.container {
  padding: 8px;
  box-sizing: border-box;
  overflow-y: auto;
  overflow-x: hidden;
}

.bar {
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
  margin-top: 60px;
}
.silderbar::-webkit-scrollbar,
.tabs::-webkit-scrollbar {
  /*滚动条整体样式*/
  width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
  height: 10px;
}
.silderbar::-webkit-scrollbar-thumb,
.tabs::-webkit-scrollbar-thumb {
  /*滚动条里面小方块*/
  border-radius: 10px;
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  background: #535353;
  border-radius: 10px;
  background-color: skyblue;
  background-image: -webkit-linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2) 25%,
    transparent 25%,
    transparent 50%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0.2) 75%,
    transparent 75%,
    transparent
  );
}
.silderbar::-webkit-scrollbar-track，.tabs::-webkit-scrollbar-track {
  /*滚动条里面轨道*/
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  border-radius: 10px;
  background: #fff;
}
.silderbar {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overflow: auto;
  float: left;
  margin: 5px;
  border: none;
}
</style>